﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

//using System.IO.Ports;
namespace Adc_Display
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(PortDataReceivedEvent);                  //添加串口数据接受事件
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;                                   //
        }

        private void PortDataReceivedEvent(object sender, SerialDataReceivedEventArgs args)                         //串口数据接收
        {
            byte Data = (byte)serialPort1.ReadByte();                                                               //读字节
            progressBar1.Value = Data;                                                                              //进度条赋值
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)                                                                                 //判断
            {
                groupBox2.Visible = false;                                                                          //第二个框框可见
                this.Size = new Size(620, 117);                                                                     //缩小
                serialPort1.Close();                                                                                //此处可不加try，catch
                button1.Text = "打开串口";
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;                                                         //串口号
                    serialPort1.Open();                                                                            //打开
                    groupBox2.Visible = true;                                                                      //第二个框框可见
                    this.Size = new Size(620, 172);                                                                //放大
                    button1.Text = "关闭串口";                                                                     //按键标题
                }
                catch
                {
                    MessageBox.Show("串口打开错误", "错误");
                }

            }
        }
    }
}
